package bb.lanxing.activity.bluetooth;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;

import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.devices.sync.XZSyncHelper;
import bb.lanxing.devices.utils.XZDeviceHelper;
import bb.lanxing.lib.devices.api.DeviceManager;
import bb.lanxing.lib.devices.api.PeerDevice;
import bb.lanxing.lib.devices.core.sync.SyncHelper;
import bb.lanxing.lib.devices.sprint.XossDevice;
import bb.lanxing.util.text.TextUtils;
import bb.lanxing.view.BiciAlertDialogBuilder;

public class GenericCyclingComputerDeviceActivity extends BaseActivity {
    private String deviceAddress;
    private String deviceSyncManagerName;
    private int deviceType;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        Intent intent = getIntent();
        Log.d("wangsm_bt"," wangsm,GenericCyclingComputerDeviceActivity onCreate: ");
        this.deviceAddress = intent.getStringExtra("EXTRA_DEVICE_ADDRESS");
        this.deviceType = intent.getIntExtra("EXTRA_DEVICE_TYPE", 0);
        if (TextUtils.isEmptyOrNull(this.deviceAddress)) {
            this.deviceAddress = XZDeviceHelper.getLastDeviceAddress(this.deviceType);
        }
        DeviceManager deviceManager = XZDeviceHelper.getDeviceManager();
        PeerDevice device = deviceManager != null ? deviceManager.getDevice(this.deviceAddress) : null;
        if (device == null || !(device instanceof XossDevice)) {
            finish();
            return;
        }
        int i = this.deviceType;
        if (i == 14) {
            this.deviceSyncManagerName = XZSyncHelper.DISCOVERY_SYNC_MANAGER_NAME;
        } else if (i == 17) {
            this.deviceSyncManagerName = XZSyncHelper.XOSS_SYNC_MANAGER_NAME;
        }
        XZDeviceHelper.setLastDeviceAddress(this.deviceType, this.deviceAddress);
        setContentView(R.layout.activity_generic_device);
        initViews(this.deviceType, this.deviceAddress);
    }

    private void initViews(int i, final String str) {
        int i2;
        setupActionBar(true);
        int i3 = R.drawable.ic_device_discovery;
        if (i == 14) {
            i2 = R.string.device_discover_title;
        } else if (i == 17) {
            i2 = R.string.device_xoss_g_title;
        } else {
            i2 = R.string.device_generic_cycling_computer_title;
            i3 = R.drawable.ic_default;
        }
        setTitle(i2);
        ((ImageView) findViewById(R.id.iv_device_logo)).setImageResource(i3);

        ((Button) findViewById(R.id.btn_disconnect)).setOnClickListener(view -> GenericCyclingComputerDeviceActivity.this.disconnect());

        ((Button) findViewById(R.id.btn_history)).setOnClickListener(view -> {
            Intent intent = new Intent(GenericCyclingComputerDeviceActivity.this, GenericSyncActivity2.class);
            intent.putExtra("extra_sync_manager", GenericCyclingComputerDeviceActivity.this.deviceSyncManagerName);
            intent.putExtra("EXTRA_DEVICE_ADDRESS", str);
            intent.putExtra(BaseSyncActivity.EXTRA_SHOULD_RUNNING_IN_BACKGROUND, true);
            GenericCyclingComputerDeviceActivity.this.startActivity(intent);
        });
    }


    public void disconnect() {
        new BiciAlertDialogBuilder(this)
                .setTitle(R.string.device_bryton_dialog_title_disconnect_ble)
                .setMessage(R.string.device_bryton_dialog_content_disconnect_ble)
                .setPositiveButton(R.string.device_bryton_dialog_btn_disconnect, (dialogInterface, i) -> {
                    XZDeviceHelper.disconnect(GenericCyclingComputerDeviceActivity.this.deviceAddress);
                    GenericCyclingComputerDeviceActivity.this.finish();
                })
                .setNegativeButton(R.string.dialog_btn_cancel, (dialogInterface, i) -> {
                }).show();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (!SyncHelper.isSynchronising(this.deviceSyncManagerName, this.deviceAddress)) {
            XZDeviceHelper.disconnect(this.deviceAddress);
        }
    }
}
